<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Handlers\SlugTranslateHandler;


class LangTranslateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $str; // 要翻译的字符串

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($str, $ttl)
    {
        $this->str = $str;

        // 延时多久后在执行队列
        $this->delay($ttl);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $resultss = '';
        $results  = getArrayKeyToValue($this->str);
        if (is_array($results[1])) {
            $resultss = getArrayKeyToValue($results[1]);
        }

        $result = '"'.isset($resultss) && !empty($resultss) ? $resultss[0].'=>'.$results[0] : $results[0].'"'.'   =>  '.'"'.isset($resultss) && !empty($resultss) ? $resultss[1].$results[1] : $results[1].'",';

//        $result = '"' . isset($resultss) && !empty($resultss) ? $resultss[0] .'=>'. $results[0] : $results[0] . '"' . '   =>  ' .
//                    '"' . app(SlugTranslateHandler::class)->translate(isset($resultss) && !empty($resultss) ? $resultss[1] . $results[1] : $results[1]) . '",';


        $dir = public_path().'/en.php';// 创建目录

        file_put_contents($dir, $result.PHP_EOL, FILE_APPEND);//写入内容
    }
}
